package com.pignest.hotel.beans;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.pignest.hotel.annotation.Bean;
import com.pignest.hotel.beans.global.BaseBean;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.LivingType;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.definition.OperateState;
import com.pignest.hotel.system.Global;

/** 
 * 入住信息
 * */
@Bean("入住")
public class Living extends BaseBean {
	private static final long serialVersionUID = -6685392774685627564L;
	
	public static final int OVERTIME_NO = 0;		//未超时
	public static final int OVERTIME_NOTIFY = 1;	//超时提醒
	public static final int OVERTIME_WARM = 2;		//超时警告
	public static final int OVERTIME_YES = 3;		//超时
	
	public static final int TEAM_STATE_NEED_PATCH = 1;
	
	/** 入住流水号, 统一使用 {@link Global#getLivineSerialNumber()}生成 */
	private String serialNumber;
	/** 当前房间 */
	private Room room;
	/** 登记会员 */
	private Member member;
	
	/** 入住时间 */
	private Date liveTime;
	/** 入住状态
	 * @see LivingState */
	private String liveState;
	/** 入住类型 
	 * @see LivingType */
	private String liveType;
	
	/** 入住登记人 */
	private String custormName;
	/** 入住登记人性别 */
	private String sex;
	/** 入住登记人电话 */
	private String phone;
	
	/** 证件号码 */
	private String passportType;
	/** 证件号 */
	private String passportNumber;
	
	/** 房间单价, 当前会员级别对应的房价 */
	private int roomPrice;
	/** 调整价, 结账时以此为准 */
	private int adjustPrice;
	/** 是否自助 */
	private boolean selfHelp;
	/** 存储的金额(以分为单位) */
	private int storage;
	/** 已经消费的金额(以分为单位) */
	private int used;
	/** 已经支付的金额(以分为单位)*/
	private int payed;
//	/** 交纳的圧金等总额(分为单位) */
//	private int pay;
//	/** 已经使用/消费的金额(分为单位) */
//	private int consume;
	/** 当前可用金额(以分为单位) */
	private int available;
	/** 房卡号 */
	private String cardNumber;
	/** 卡授权结束时间 */
	private Date grantDate;
	/** 预计离店时间 */
	private Date expectLeaveDate;
	/** 离店时间 */
	private Date leaveDate;
	/** 合作单位 */
	private String cooperationUnit;
	/** 开房方式*/
	private String openRoomType;
	/** 备注 */
	private String remark;
	
	private int overtime;
	
	private TbCloseAccounts tbColoseAccounts;
	
	private ProtocolUnit protocolUnit;
	
	/** 预订 */
	private Booking booking;
	private Set<LivingSpecifics> specificses;
	
	private Set<TbAccounts> accounts;
	
	/** 是否主要负责人(团队) */
	private boolean master = true;
	/** 团队状态(是否需要补全信息) */
	private int teamState;
	
	private String roomId;
	
	public Living() {
	}
	
	public Member getMember() {
		return this.member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Room getRoom() {
		return this.room;
	}

	public void setRoom(Room room) {
		this.room = room;
		this.roomId = roomId == null ? null : room.getId();
	}

	public int getStorage() {
		return this.storage;
	}

	public void setStorage(int storage) {
		this.storage = storage;
	}

	public int getUsed() {
		return this.used;
	}

	public void setUsed(int used) {
		this.used = used;
	}

	public int getPayed() {
		return payed;
	}

	public void setPayed(int payed) {
		this.payed = payed;
	}

	public int getAvailable() {
		return this.available;
	}

	public void setAvailable(int available) {
		this.available = available;
	}

	public String getLiveState() {
		return this.liveState;
	}

	public void setLiveState(String state) {
		this.liveState = state;
	}

	public String getLiveType() {
		return this.liveType;
	}

	public void setLiveType(String livetype) {
		this.liveType = livetype;
	}

	public Booking getBooking() {
		return this.booking;
	}

	public void setBooking(Booking booking) {
		this.booking = booking;
	}
	
	public String getSerialNumber() {
		return serialNumber;
	}
	
	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}

	public String getCustormName() {
		return custormName;
	}

	public void setCustormName(String custormName) {
		this.custormName = custormName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPassportType() {
		return passportType;
	}

	public void setPassportType(String passportType) {
		this.passportType = passportType;
	}

	public String getPassportNumber() {
		return passportNumber;
	}

	public void setPassportNumber(String passportNumber) {
		this.passportNumber = passportNumber;
	}

	public int getRoomPrice() {
		return roomPrice;
	}

	public void setRoomPrice(int roomPrice) {
		this.roomPrice = roomPrice;
	}

	public boolean isSelfHelp() {
		return selfHelp;
	}

	public void setSelfHelp(boolean isSelfHelp) {
		this.selfHelp = isSelfHelp;
	}

	public String getCardNumber() {
		return cardNumber;
	}

	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}

	public Date getGrantDate() {
		return grantDate;
	}

	public void setGrantDate(Date grantDate) {
		this.grantDate = grantDate;
	}

	public Date getLeaveDate() {
		return leaveDate;
	}
	
	public void setLeaveDate(Date leaveDate) {
		this.leaveDate = leaveDate;
	}
	
	public Date getLiveTime() {
		return liveTime;
	}
	
	public void setLiveTime(Date liveTime) {
		this.liveTime = liveTime;
	}
	
	public void setExpectLeaveDate(Date expectLeaveDate) {
		this.expectLeaveDate = expectLeaveDate;
	}
	
	public Date getExpectLeaveDate() {
		return expectLeaveDate;
	}
	
	public int getAdjustPrice() {
		return adjustPrice;
	}
	
	public void setAdjustPrice(int adjustPrice) {
		this.adjustPrice = adjustPrice;
	}
	
	public void setCooperationUnit(String cooperationUnit) {
		this.cooperationUnit = cooperationUnit;
	}
	public String getCooperationUnit() {
		return cooperationUnit;
	}
	
	public String getRemark() {
		return remark;
	}
	
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public Set<LivingSpecifics> getSpecificses() {
		return specificses;
	}
	
	public void setSpecificses(Set<LivingSpecifics> specificses) {
		this.specificses = specificses;
	}
	
	public int getOvertime() {
		return overtime;
	}
	
	public void setOvertime(int overtime) {
		this.overtime = overtime;
	}
	

	public TbCloseAccounts getTbColoseAccounts() {
		return tbColoseAccounts;
	}

	public void setTbColoseAccounts(TbCloseAccounts tbColoseAccounts) {
		this.tbColoseAccounts = tbColoseAccounts;
	}

	public ProtocolUnit getProtocolUnit() {
		return protocolUnit;
	}

	public void setProtocolUnit(ProtocolUnit protocolUnit) {
		this.protocolUnit = protocolUnit;
	}

	public int getTeamState() {
		return teamState;
	}
	
	public void setTeamState(int teamState) {
		this.teamState = teamState;
	}
	
	public boolean isMaster() {
		return master;
	}
	public String getMaster2(){
		if(master)
			return "是";
		else
			return "否";
	}
	public void setMaster(boolean master) {
		this.master = master;
	}
	public String getRoomId() {
		return roomId;
	}
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}
	
	public boolean updateOvertime(long now) {
		if (liveState == null || !(LivingState.LIVING.is(liveState) || LivingState.PARTCHECKOUT.is(liveState)))
			return false;
		if (grantDate == null)
			return false;
		if (now == 0L)
			now = System.currentTimeMillis();
		int old = overtime;
		int timeout = -1;
		long left = grantDate.getTime() - now;
		if (left < 0L) {
			timeout = OVERTIME_YES;
		} else {
			if (OpenRoomType.DAY.is(openRoomType)) {
				if (left < 30L * 6000) {		//30 m
					timeout = OVERTIME_WARM;
//				} else if (left < 60L * 6000) {	//60 m
//					timeout = OVERTIME_NOTIFY;
				} else {
					timeout = OVERTIME_NO;
				}
			} else if (OpenRoomType.HALFDAY.is(openRoomType)) {
				if (left < 60L * 6000) {		//30 m
					timeout = OVERTIME_WARM;
//				} else if (left < 60L * 6000) {	//60 m
//					timeout = OVERTIME_NOTIFY;
				} else {
					timeout = OVERTIME_NO;
				}
			} else if (OpenRoomType.HOURS.is(openRoomType)) {
				if (left < 60L * 6000) {		//30 m
					timeout = OVERTIME_WARM;
//				} else if (left < 60L * 6000) {	//60 m
//					timeout = OVERTIME_NOTIFY;
				} else {
					timeout = OVERTIME_NO;
				}
			}
		}
		if (timeout != -1)
			this.overtime = timeout;
		return old != timeout;
	}
	
	public LivingSpecifics renderSpecifics() {
		LivingSpecifics spec = new LivingSpecifics();
		spec.setRoom(room.getName());
		spec.setGrantDate(grantDate);
		spec.setLeaveDate(expectLeaveDate);
		spec.setLiving(this);
		spec.setOperateDate(liveTime);
		spec.setOperateType(OperateState.LIVING.name());
		spec.setPay(storage);
		/*String test="";
		spec.setPay(Integer.parseInt(test));*/
		spec.setRoomPrice(adjustPrice);
		if (specificses == null)
			specificses = new HashSet<LivingSpecifics>();
		specificses.add(spec);
		return spec;
	}

	public Set<TbAccounts> getAccounts() {
		return accounts;
	}

	public void setAccounts(Set<TbAccounts> accounts) {
		this.accounts = accounts;
	}

	public String getOpenRoomType() {
		return openRoomType;
	}

	public void setOpenRoomType(String openRoomType) {
		this.openRoomType = openRoomType;
	}
    public String getRespectfully()
    {
    	if(sex.equals("男")){
    		return "先生";
    	}else
    		return "小姐/女士";
    }
	@Override
	public String toSimpleString() {
		return concat(room.getName(), String.valueOf(roomPrice), custormName);
	}
}